package com.wonder.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wonder.app.domain.PrdFeed;
import com.wonder.app.domain.PrdFeedDetail;
import com.wonder.app.domain.PrdReportInspect;
import com.wonder.app.domain.PrdReportInspectDetail;
import com.wonder.app.mapper.PrdFeedDao;
import com.wonder.app.mapper.PrdFeedDetailDao;
import com.wonder.app.mapper.PrdReportInspectDao;
import com.wonder.app.mapper.PrdReportInspectDetailDao;
import com.wonder.app.request.PrdFeedRequest;
import com.wonder.app.request.PrdReportInspectRequest;
import com.wonder.app.service.PrdFeedService;
import com.wonder.app.service.PrdReportInspectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PrdReportInspectImpl extends ServiceImpl<PrdReportInspectDao, PrdReportInspect> implements PrdReportInspectService {

    @Autowired
    private PrdReportInspectService prdReportInspectService;
    @Autowired
    private  PrdReportInspectDao prdReportInspectDao;
    @Autowired
    private PrdReportInspectDetailDao prdReportInspectDetailDao;

    @Override
    public IPage<PrdReportInspect> selectList(PrdReportInspectRequest prdReportInspectRequest, Page<PrdReportInspect> page) {
        LambdaQueryWrapper<PrdReportInspect> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (prdReportInspectRequest.getFBillNo() != null){
            lambdaQueryWrapper.like(PrdReportInspect::getFBillNo,prdReportInspectRequest.getFBillNo());
        }

        if (prdReportInspectRequest.getFKDBILLNO() != null){
            lambdaQueryWrapper.like(PrdReportInspect::getFKDBILLNO,prdReportInspectRequest.getFKDBILLNO());
        }

        if (prdReportInspectRequest.getFBILLNOMO() != null){
            lambdaQueryWrapper.like(PrdReportInspect::getFBILLNOMO,prdReportInspectRequest.getFBILLNOMO());
        }

        if (prdReportInspectRequest.getInspectUser() != null){
            lambdaQueryWrapper.like(PrdReportInspect::getInspectUser,prdReportInspectRequest.getInspectUser());
        }

        if (prdReportInspectRequest.getFCREATEUSER() != null){
            lambdaQueryWrapper.like(PrdReportInspect::getFCREATEUSER,prdReportInspectRequest.getFCREATEUSER());
        }

        if (prdReportInspectRequest.getInspectDate() != null){
            Date[] orderDate = prdReportInspectRequest.getInspectDate();
            if(orderDate.length>0)
                lambdaQueryWrapper.ge(PrdReportInspect::getInspectDate, orderDate[0]);
            if(orderDate.length==2)
                lambdaQueryWrapper.le(PrdReportInspect::getInspectDate,orderDate[1]);
        }
        lambdaQueryWrapper.orderByDesc(PrdReportInspect::getInspectDate);

        if (prdReportInspectRequest.getFDATE() != null){
            Date[] orderDate = prdReportInspectRequest.getFDATE();
            if(orderDate.length>0)
                lambdaQueryWrapper.ge(PrdReportInspect::getFDATE, orderDate[0]);
            if(orderDate.length==2)
                lambdaQueryWrapper.le(PrdReportInspect::getFDATE,orderDate[1]);
        }
        lambdaQueryWrapper.orderByDesc(PrdReportInspect::getFDATE);


        if (prdReportInspectRequest.getCreateTime() != null){
            Date[] orderDate = prdReportInspectRequest.getCreateTime();
            if(orderDate.length>0)
                lambdaQueryWrapper.ge(PrdReportInspect::getCreateTime, orderDate[0]);
            if(orderDate.length==2)
                lambdaQueryWrapper.le(PrdReportInspect::getCreateTime,orderDate[1]);
        }
        lambdaQueryWrapper.orderByDesc(PrdReportInspect::getCreateTime);
        Page<PrdReportInspect> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return selectPage;
    }
    /**
     * 汇报检验单明细详情
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getPrdReportInspectDetail(Map<String, Object> map) {
        String billNo = map.get("billNo").toString();
        PrdReportInspect prdreportinspectform = prdReportInspectDao.selectOne(new LambdaQueryWrapper<PrdReportInspect>()
                .eq(PrdReportInspect::getFBillNo,billNo));
        List<PrdReportInspectDetail> prdreportinspectdetailList = prdReportInspectDetailDao.selectList(new LambdaQueryWrapper<PrdReportInspectDetail>()
                .eq(PrdReportInspectDetail::getFBillNo,billNo));
        Map<String,Object> data = new HashMap<>();
        data.put("data",prdreportinspectform);
        data.put("dataList",prdreportinspectdetailList);
        return data;
    }
}

